import Vue from "vue"
import Vuex from "vuex"
import appList from "@/store/appList"
import config from "@/store/config"
import {getCache, setCache} from "@/utils/jsUtils"

Vue.use(Vuex)

let zIndex = 3, id$0 = 3


export default new Vuex.Store({
  state: {
    config: config,  // 项目配置文件
    windowList: [
      {
        id: 1100,
        appID: 1100,
        title: "桌面",
        is: "Desktop",
        allowMulti: false,
        zIndex: -1,
        meta: {
          cpu: 30
        }
      },
    ],  // 窗口列表
    appList: appList,  // 应用列表
    user: getCache("user") || {},  // 保存用户信息
    context: {
      client: {
        x: "",
        y: "",
        w: "",
        h: "",
      },
      list: [],
      show: false,
    },  // 右键菜单信息
    picList: [],  // 预览图片列表, 提供给app:Picture使用 todo 待优化
    musicHis: getCache("musicHis") || [],  // 历史歌曲
    likeList: getCache("likeList") || [],   // 我喜欢的格式
    likeSinger: getCache("likeSinger") || []  //关注的歌手
  },
  mutations: {
    addWindow(state, window) {
      state.windowList.push({
        minimize: false,
        ...window,
        zIndex: zIndex++,
        id: id$0++,
        alive: true,
      })
    },
    delWindow(state, id) {
      state.windowList.splice(state.windowList.findIndex(item => item.id === id), 1)
    },
    addWindowSurvivalTime(state, id) {
      let target = state.appList.find(item => item.appID === id)
      if (target) {
        Vue.prototype.$set(target, 'survivalTime', target.survivalTime ? target.survivalTime + 1 : 1)
        //target.survivalTime = target.survivalTime ? target.survivalTime + 1 : 1
      }
    },
    setWindowMinimize(state, {id, flag}) {
      let target = state.windowList.find(item => item.id === id)
      if (flag) {
        target.zIndex = -1
      }
      target.minimize = flag
    },
    setWindowZIndex(state, id) {
      let target = state.windowList.find(item => item.id === id)
      target.minimize = false
      target.zIndex = zIndex++
    },
    setMinZIndex(state, id) {
      let target = state.windowList.find(item => item.id === id)
      target.zIndex = -1
    },
    setUser(state, user) {
      state.user = user
      setCache("user", user)
    },
    setConfig(state, {field, value}) {
      state.config[field] = value
    },
    setContext(state, payload) {
      state.context = payload
    },
    setPicList(state, list){
      state.picList = list
    },
    addMusicHis(state, songObj) {
      let index = state.musicHis.findIndex(item=>item.mid===songObj.mid)
      if(index !== -1){
        return
      }
      state.musicHis.unshift(songObj)
      if(state.musicHis.length > 100){
        state.musicHis.pop()
      }
      setCache("musicHis", state.musicHis)
    },
    toggleLike(state, songObj) {
      let index = state.likeList.findIndex(item=>item.mid===songObj.mid)
      if(index===-1){
        state.likeList.unshift(songObj)
      }else{
        state.likeList.splice(index, 1)
      }
      setCache("likeList", state.likeList)
    },
    toggleLikeSinger(state, singerObj){
      let index = state.likeSinger.findIndex(item=>item.mid===singerObj.mid)
      if(index===-1){
        state.likeSinger.unshift(singerObj)
      }else{
        state.likeSinger.splice(index, 1)
      }
      setCache("likeSinger", state.likeSinger)
    }
  },
  actions: {
    // 应用间传递数据
    sendMsg(context, {from/*vm*/, to/*appID*/, data}) {
      // data: {type:"", xxx}
      let target = context.state.windowList.filter(item=> item.appID === to)
      return new Promise((resolve, reject) => {
        if(target.length){
          target.forEach(target => {
            Vue.prototype.$msg.primary(`跨应用传递消息--->${target.title}`)
            let windowVm = target.vm
            windowVm.onReceiveMsg(from, data)
          })
        }else{
          reject(false)
        }
      })
    },

    // 连接应用
    connectWindowAndVm(context, {windowID, vm}) {
      let target = context.state.windowList.find(item => item.id === windowID)
      if(target){
        target.vm = vm
      }
    },

    // 打开应用
    openApp({state, commit}, {appID}) {
      let target = state.appList.find(item => item.appID === appID)
      if (target.allowMulti) {
        commit("addWindow", target)
        Vue.prototype.$msg.success(`打开应用--->${target.title}`)
      } else {
        let existenceAlive = state.windowList.find(item => item.appID === appID)
        if (existenceAlive) {
          commit("setWindowZIndex", existenceAlive.id)
        } else {
          Vue.prototype.$msg.success(`打开应用--->${target.title}`)
          commit("addWindow", target)
        }
      }
    },

    // 关闭应用
    closeApp({commit}, {window, client}) {
      let id = window.id
      const target = appList.find(item => item.appID === window.appID)
      target.client = {...target.client, ...client}
      commit("delWindow", id)
    },

    // 统计CPU时间
    statisticalAppData({state, commit}) {
      state.windowList.forEach(window => {
        commit("addWindowSurvivalTime", window.appID)
      })
    },

    // 打开右键菜单
    openContext(context, payload) {
      let {x = 0, y = 0, w = 200, list} = payload
      list.push({label: "取消"})
      context.commit("setContext", {
        client: {x: x + 3, y: y + 3, w},
        list,
        show: true
      })
    },

    // 关闭右键菜单
    closeContext(context) {
      context.state.context.show = false
    },

    // 图片预览
    openAppPicture(context, payload){
      context.commit("setPicList", payload.list)
      context.dispatch("openApp", {appID: 1101})
    },

  },
  modules: {}
})
